<?php

function getLastCourses($count=10, $view_mode='teaser'){
	$build = array();
	$query = db_select('node', 'n')->extend('PagerDefault');
	
	$nids = $query->fields('n', array('nid', 'sticky', 'created'))
	       ->condition('type', 'course')
	       ->condition('status', 1)
	       ->orderBy('sticky', 'DESC')
	       ->orderBy('created', 'DESC')
	       ->limit('10')
	       ->addTag('node_access')
	       ->execute()
	       ->fetchCol();
    
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes, $view_mode);
    /*
    $build['pager'] = array(
    	'#theme' => 'pager',
    	'#weight' => 5
    );
    */
    return $build;
}

function getHotCourses($count=10, $view_mode='teaser'){
	$build = array();
	$query = db_select('node', 'n')->extend('PagerDefault');
	
	$nids = $query->fields('n', array('nid', 'sticky', 'created'))
	       ->condition('type', 'course')
	       ->condition('status', 1)
	       ->orderBy('sticky', 'DESC')
	       ->orderBy('created', 'DESC')
	       ->limit(variable_get('default_nodes_main', 10))
	       ->addTag('node_access')
	       ->execute()
	       ->fetchCol();
    
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes, $view_mode);
    /*
    $build['pager'] = array(
    	'#theme' => 'pager',
    	'#weight' => 5
    );
    */
    return $build;
}

/**
 * Fetch popular courses and group by knowlage category.
 * working:
 * Get course date by knowlage category
 * */
function getHotCoursesByCategory($count=5, $view_mode='teaser'){
	$build = array();
	$query = db_select('node', 'n')->extend('PagerDefault');
	
	$nids = $query->fields('n', array('nid', 'sticky', 'created'))
	       ->condition('type', 'course')
	       ->condition('status', 1)
	       ->orderBy('sticky', 'DESC')
	       ->orderBy('created', 'DESC')
	       ->limit(variable_get('default_nodes_main', 10))
	       ->addTag('node_access')
	       ->execute()
	       ->fetchCol();
    
    $nodes = node_load_multiple($nids);
    $build += node_view_multiple($nodes, $view_mode);
    /*
    $build['pager'] = array(
    	'#theme' => 'pager',
    	'#weight' => 5
    );
    */
    return $build;
}
/**
 * The function used for alter node element for course content type
 * 
 * What you can do with this function?
 *   You can alter the $node object for view the node.
 * 
 * 1. view theme is different for node list page and detail page.
 *    Supported view mode list:
 *    "teaser" [default supported] for node list page.
 *    "full"   [default supported] for node list page.
 *    "mini"   [private] for course title list with some special feature.
 *    "clear"  [private] for clear title list without anything else.
 * 
 * @param object, node object
 * @param string, default is 'node', It is hard code, So the parameter is nonsense.
 * @param multiple, extra parameter, Generally it is nonsense too.
 *   
 * */
function course_node_view_alter(&$node, $type, $content2){
	if(!is_array($node)){
		return false;
	}
	if('course' == $node['#node']->type){
		_course_node_view_alter($node);
	}
	
}

function _course_node_view_alter(&$node){
	static $show;
	if(empty($show)){
		//echo '<pre>';
		$node['field_thumbnail']['width'] = '80';
		$node['field_thumbnail']['height'] = '80';
		//var_dump($node['field_thumbnail']);
		//echo '</pre>';
		$show = 1;
	}
	/*	
	if('RichTitle' != $node['#view_mode']){
		return ;
	}
	var_dump($node->field_thumbnail);*/
}


/**
 * Retrieve user's favorite  courses
 * */
function getMyFavoriteCourses($user){
	
}
